package com.demo.dao.impl;

import com.demo.dao.ClassDao;
import com.demo.entity.Class;
import com.demo.entity.Student;
import com.demo.util.Util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassDaoImpl implements ClassDao {

//    @Override
    public void add(Class vo) {
        //id时自增的不用加
        String sql = "insert into `classes` (`classId`,`className`,`major`,`depid`,`grade`) values(?,?,?,?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);
// TODO: 2023/11/11
            ps.setLong(1, vo.getClassId());
            ps.setString(2, vo.getClassName());
            ps.setString(3, vo.getMajor());
            ps.setString(4, vo.getDepid());
            ps.setString(5, vo.getGrade());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//        @Override
        public void update(Class vo) {
        //你可以直接复制右边这个表
        //三个问号要传递三个值
        String sql = "update `classes` set `className` = ? ,`major` = ? , `depid` = ? , `grade` = ?  where `classId` = ?";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            //这不就是三个值吗
            // TODO: 2023/11/11 先 get 再 set
            ps.setString(1, vo.getClassName());
            ps.setString(2, vo.getMajor());
            ps.setString(3, vo.getDepid());
            ps.setString(4, vo.getGrade());
            ps.setLong(5, vo.getClassId());

            System.out.println(vo.getClassId());
            System.out.println(vo.getClassName());
            System.out.println(vo.getMajor());
            System.out.println(vo.getDepid());
            System.out.println(vo.getGrade());
            System.out.println(sql);


            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
    public void delete(long classId) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `classes` where classId = " + classId;
            s.execute(sql);
            s.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
    public Class get(Serializable classId) {
        // TODO: 2023/11/11
        Class vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `classes` where classId = " + classId;
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()) {
                vo = new Class();
                // TODO: 2023/11/11
                vo.setClassId(rs.getLong("classId"));
                vo.setClassName(rs.getString("className"));
                vo.setMajor(rs.getString("major"));
                vo.setDepid(rs.getString("depid"));
                vo.setGrade(rs.getString("grade"));

            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

//    @Override
    public Map<String, Object> list(Map<String, Object> params) {
        // TODO: 2023/11/11
        List<Class> list = new ArrayList();

        int totalCount = 0;
        String condition = "";
        String sqlList;
        if (params.get("searchColumn") != null && !"".equals(params.get("searchColumn"))) {
            condition += " and `" + params.get("searchColumn") + "` like '%" + params.get("keyword") + "%'";
        }
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps;
            ResultSet rs;
            String limit = (params.get("startIndex") != null && params.get("pageSize") != null) ? " limit " + params.get("startIndex") + "," + params.get("pageSize") : "";
            // TODO: 2023/11/12
            sqlList = "select * from `classes` where 1=1 " + condition + " order by classId asc " + limit + ";";
            ps = c.prepareStatement(sqlList);
            rs = ps.executeQuery();
            while (rs.next()) {

                // TODO: 2023/11/11
                Class vo = new Class();
                vo.setClassId(rs.getLong("classId"));
                vo.setClassName(rs.getString("className"));
                vo.setMajor(rs.getString("major"));
                vo.setDepid(rs.getString("depid"));
                vo.setGrade(rs.getString("grade"));
                list.add(vo);
            }
            // TODO: 2023/11/11
            String sqlCount = "select count(*) from `classes` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }
}
